package netip;

import java.net.InetAddress;
import java.net.UnknownHostException;

//没有封装端口
public class InetDemo01 {
    public static void main(String[] args) {
        try {
            //得到本机地址
            InetAddress addr=InetAddress.getLocalHost();
            System.out.println(addr);//计算机名/本机ip
            System.out.println(addr.getHostAddress());//获取本机ip
            System.out.println(addr.getHostName());//获取本机名
            //根据域名得到InetAddress对象
            addr=InetAddress.getByName("www.163.com");
            System.out.println(addr.getHostAddress());//返回163服务器的ip
            System.out.println(addr.getHostName());//输出www.163.com
            //根据ip得到InetAddress对象
            addr=InetAddress.getByName("106.225.223.76");
            System.out.println(addr.getHostAddress());//返回163服务器的ip
            System.out.println(addr.getHostName());//ip不存在或者域名不给解析返回的还是服务器ip
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
